<?php
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../DAO/TbluserDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Util/Tool.php';
class UserService
{

	public function findByOid(BaseContext $context, ServiceResult $result, $oid)
	{
		$userDomain = null;
		try 
		{
			$userDao = new TbluserDAO($context);
			$userDomain = $userDao->findByOid($oid);
			$result->setSuccess();
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
		
		return $userDomain;
	}
	
	
	public function changePass(BaseContext $context, ServiceResult $result, $newpass, $id)
	{
		try 
		{
			$newpass = Tool::md5_base64($newpass);
			$userDao = new TbluserDAO($context);
			$userDomain = $userDao->changePass($newpass, $id);	
			$result->setSuccess();
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	public function checkPassExists(BaseContext $context, ServiceResult $result, $pass, $id)
	{
		try 
		{
			$userDao = new TbluserDAO($context);
			$rs= $userDao->checkPassExists($pass,$id);										
			if(!Tool::isEmpty($rs))
			{
				return true;
			}
			else 
			{
				return false;
			}
		} 
		
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
		}
	}
	public function createUser(BaseContext $context, ServiceResult $result,TbluserDomain $userDomain) {
		try 
		{
			$userDao = new TbluserDAO($context);
			$userDao->create($userDomain);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
	public function updateUser(BaseContext $context, ServiceResult $result,TbluserDomain $userDomain) {
		try 
		{
			$userDao = new TbluserDAO($context);
			$userDao->update($userDomain);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
	public function deleteUser(BaseContext $context, ServiceResult $result,$userID) {
		try 
		{
			$userDao = new TbluserDAO($context);
			$userDao->deleteByOid($userID);
			$result->setSuccess();				
			return true;	
		} 
		catch (Exception $e) 
		{
			$context->logException($e);
			$result->setException($e);
			return false;
		}
	}
}